package cn.itcast.itcaststore.web.servlet.manager;

import cn.itcast.itcaststore.domain.Orders;
import cn.itcast.itcaststore.serive.OrderService;
import cn.itcast.itcaststore.serive.impl.OrderServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 删除订单
 */
@WebServlet("/admin/delOrderById")
public class DelOrderByIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取用户类型
        String type = req.getParameter("type");
        //2.获取要查询订单的id
        String orderId = req.getParameter("id");
        //3.创建Service层对象,并根据id查询订单
        OrderService orderService = new OrderServiceImpl();
        Orders order = orderService.findOrderById(orderId);
        //4.将查询出的订单信息添加到request作用域中
        req.setAttribute("order", order);
        //5.如果用户类型不为null，则请求转发到view.jsp页面
        //否则转发到orderInfo.jsp页面
        if (type != null && type.trim().length() > 0) {
            //后台系统，调用service层delOrderById()方法删除订单
            orderService.delOrderById(orderId);
            if ("admin".equals(type)) {
                req.getRequestDispatcher("/admin/findOrders").forward(req, resp);
            }
            return;
        } else {
            //前台系统调用service层delOrderByIdWithClient()方法删除相应订单
            orderService.delOrderByIdWithClient(orderId);
        }
        resp.sendRedirect(req.getContextPath() + "/client/delOrderSuccess.jsp");
        return;
    }
}
